﻿using System;
using System.Text.RegularExpressions;
using System.Web.Routing;
using TwitterBootstrapMVC;
using Web.UI.AdminLTE.Common;
using Web.UI.TypeExtensions;

namespace Web.UI.AdminLTE.Controls.Widget
{
    public class BoxHeader : HtmlElementBase
    {
        private string _title;
        private Icon _icon;

        public BoxHeader(string title)
        {
            _title = title;
            EnsureClass("box-header");
        }

        public BoxHeader Title(string title)
        {
            _title = title;
            return this;
        }

        public BoxHeader Tooltip(string tooltip)
        {
            MergeHtmlDataAttributes(new RouteValueDictionary
            {
                {"toggle", "tooltip"},
                {"original-title", tooltip},
            });
            return this;
        }

        public BoxHeader Icon(Icon icon)
        {
            _icon = icon;
            return this;
        }

        public BoxHeader Icon(string icon)
        {
            return Icon(new Icon(icon));
        }

        public override string ToHtmlString()
        {
            var tag = TagBuilderExtensions.New("div").MergeHtmlAttributes(HtmlAttr.FormatHtmlAttributes());
            if (_icon != null)
            {
                tag.AppendHtml(_icon);
            }
            // Trick: remove end tag temporary. End tag will be rendered when disposing its parent (BoxHeaderPanel)
            return tag.AppendHtml(string.Format("<h3 class=\"box-title\">{0}</h3>", _title))
                      .ToString().TrimEnd("</div>");
        }
    }
}